package com.example.drawboard.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class LineStyleView extends View {

    private int lineWeight = 2;
    private int color =0xFF000000;

    private Paint paintLine;

    private int height,width;

    public LineStyleView(Context context) {
        super(context);
        init();
    }

    public LineStyleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LineStyleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
        paintLine.setColor(color);
        paintLine.setStrokeWidth(lineWeight);
        paintLine.setStyle(Paint.Style.STROKE);
        paintLine.setStrokeCap(Paint.Cap.ROUND);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawLine(0,height/2,width,height/2,paintLine);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //获取宽的测量模式
        int heightSize =  MeasureSpec.getSize(heightMeasureSpec);//测量值
        height= resolveSize(heightSize, heightMeasureSpec);
        int widthSize =  MeasureSpec.getSize(widthMeasureSpec);//测量值
        width= resolveSize(widthSize, widthMeasureSpec);
        setMeasuredDimension(width,height);
    }

    public void setLineWeight(int lineWeight) {
        paintLine.setStrokeWidth(lineWeight);
        this.lineWeight = lineWeight;
        invalidate();
    }

    public void setColor(int color) {
        this.color = color;
        paintLine.setColor(color);
        invalidate();
    }


}
